package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.adapter.GuideBannerAdapter;
import com.xijia.wuyouwallet.base.BaseActivity;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class GuideActivity extends BaseActivity implements View.OnClickListener{

    private TextView guide_tv_time;
    private boolean isStartTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
    }
    //初始化View
    private void initView() {
        ViewPager guide_vp = (ViewPager) findViewById(R.id.guide_vp);
        final RadioGroup guide_rg = (RadioGroup) findViewById(R.id.guide_rg);
        guide_tv_time = (TextView) findViewById(R.id.guide_tv_time);

        final int[] list = {R.mipmap.guide001, R.mipmap.guide002, R.mipmap.guide003};
        for (int i = 0; i < list.length; i++) {
            LayoutInflater.from(GuideActivity.this).inflate(R.layout.guide_rg_bg, guide_rg);
        }
        guide_rg.check(guide_rg.getChildAt(0).getId());
        guide_vp.setAdapter(new GuideBannerAdapter(GuideActivity.this, list));
        guide_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                guide_rg.check(guide_rg.getChildAt(position).getId());
                if (position == list.length - 1 && !isStartTime) {
                    isStartTime = true;
                    final int[] number = {3};
                    guide_tv_time.setVisibility(View.VISIBLE);
                    guide_tv_time.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            number[0] = -1;
                            jumpToPage(LoginActivity.class);
                            finish();
                        }
                    });
                    new Thread() {
                        @Override
                        public void run() {
                            while (number[0] >= 0) {
                                try {
                                    if (number[0] == 0) {
                                        Message msg = Message.obtain();
                                        msg.what = 1;
                                        msg.obj = number[0];
                                        handler.sendMessage(msg);
                                        return;
                                    }
                                    Message msg = Message.obtain();
                                    msg.what = 1;
                                    msg.obj = number[0];
                                    handler.sendMessage(msg);
                                    number[0]--;
                                    sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }.start();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    @Override
    public void onClick(View view) {

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    int number = (int) msg.obj;
                    if (number != 0) {
                        guide_tv_time.setText(String.valueOf(number) + "s 跳过");
                    } else {
                        jumpToPage(LoginActivity.class);
                        finish();
                    }
                    break;
            }
        }
    };
}
